package com.lovo.web.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.commom.remoteMethod.ISysProductRemote;
import com.lovo.web.bean.*;
import com.lovo.web.mapper.*;
import com.lovo.web.service.IBuyCarService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BuyCarServiceImpl implements IBuyCarService {
    @Autowired
    private IBuyCarMapper buyCarMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserOrderMapper orderMapper;
    @Autowired
    private IUserAddressMapper addressMapper;
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IProductInfoMapper productInfoMapper;
    @Autowired
    private ISysProductRemote productRemote;
    @Override
    public void add(BuyCarBean buyCar) {
        QueryWrapper<BuyCarBean> queryWrapper=new QueryWrapper();
        queryWrapper.eq("fk_userId", buyCar.getUserId());
        queryWrapper.eq("b_goodsName",buyCar.getGoodsName());
        BuyCarBean oldBuyCar=buyCarMapper.selectOne(queryWrapper);
        //如果该购物车记录不存在，新增购物车记录,并将购买数量设置为1
        if (oldBuyCar==null){
            buyCar.setNum(1);
            buyCarMapper.insert(buyCar);
            return;
        }
        //如果该购物车记录存在，修改购物车数量+1
        oldBuyCar.setNum(oldBuyCar.getNum()+1);
        buyCarMapper.updateById(oldBuyCar);
    }

    @Override
    public List<BuyCarBean> findAllByUserId(long userId) {
        QueryWrapper<BuyCarBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_userId", userId);
        List<BuyCarBean> list=buyCarMapper.selectList(queryWrapper);
        for (BuyCarBean buyCar:list){
            //查询商品
            QueryWrapper<ProductBean> productBeanQueryWrapper=new QueryWrapper<>();
            productBeanQueryWrapper.eq("p_productName", buyCar.getGoodsName());
            buyCar.setProduct(productMapper.selectOne(productBeanQueryWrapper));
            //查询商品详情
            QueryWrapper<ProductInfoBean> productInfoBeanQueryWrapper=new QueryWrapper<>();
            productInfoBeanQueryWrapper.eq("pi_productName", buyCar.getGoodsName());
            productInfoBeanQueryWrapper.eq("pi_specs", buyCar.getSpecs());
            buyCar.setProductInfo(productInfoMapper.selectOne(productInfoBeanQueryWrapper));
        }
        return list;
    }

    @Override
    public void updateNumByBuyCarId(long buyCarId, int newNum) {
        QueryWrapper<BuyCarBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pk_buyCarId", buyCarId);
        BuyCarBean buyCar=buyCarMapper.selectOne(queryWrapper);
        buyCar.setNum(newNum);
        buyCarMapper.updateById(buyCar);
    }

    @Override
    public void delByBuyCarId(long buyCarId) {
        buyCarMapper.deleteById(buyCarId);
    }

    @Override
    public String delBuyCarAndCreateOrder(List<Long> buyCarIdList, List<UserOrderBean> orderList) {
        //生成订单并发送给审核、后台系统
        for (UserOrderBean order:orderList){
           String msg=this.createOrder(order);
           if (!"ok".equals(msg)){
               return msg;
           }
        }
        //删除购物车
        for (long buyCarId:buyCarIdList) {
            buyCarMapper.deleteById(buyCarId);
        }
        return "下单成功";
    }

    @Override
    public String buyProduct(UserOrderBean order) {
        String msg=this.createOrder(order);
        if (!"ok".equals(msg)){
            return msg;
        }
        return "下单成功";
    }

    private String createOrder(UserOrderBean order){
        //查询商品详情
        QueryWrapper<ProductInfoBean> productInfoQueryWrapper=new QueryWrapper<>();
        productInfoQueryWrapper.eq("pi_productName", order.getGoodsName());
        productInfoQueryWrapper.eq("pi_specs", order.getSpecs());
        ProductInfoBean productInfo=productInfoMapper.selectOne(productInfoQueryWrapper);
        if (productInfo.getInventory()<order.getGoodsNum()){
            return productInfo.getProductName()+productInfo.getSpecs()+"库存不足，请修改购买数量";
        }
        //减去订单中的购买数量
        productInfo.setInventory(productInfo.getInventory() - order.getGoodsNum());
        productInfoMapper.updateById(productInfo);

        //查询商品
        QueryWrapper<ProductBean> productQueryWrapper=new QueryWrapper<>();
        productQueryWrapper.eq("p_productName", order.getGoodsName());
        ProductBean product=productMapper.selectOne(productQueryWrapper);
        //减去订单中的购买数量
        product.setNum(product.getNum()-order.getGoodsNum());
        productMapper.updateById(product);
        //保存订单到本系统数据库
        orderMapper.insert(order);
        //发送订单到审核系统的订单队列
        rabbitTemplate.convertAndSend("orderExchange","cscOrder.key", JSON.toJSONString(order));
        //发送订单到后台系统的订单队列
        rabbitTemplate.convertAndSend("orderExchange","sscSysOrder.key", JSON.toJSONString(order));
        //调用后台接口修改商品库存---减去订单中的购买数量
        productRemote.outKu(product.getProductName(),productInfo.getSpecs(),order.getGoodsNum());

        return "ok";
    }
}
